package com.zmn.oms.dubbo.impl.zmn.normal.erp;

import com.google.common.collect.Lists;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.oms.dubbo.dto.zmn.erp.EngineerShareDRO;
import com.zmn.oms.dubbo.interfaces.zmn.normal.erp.EngineerShareListRemoteService;
import com.zmn.oms.dubbo.utils.DubboConsts;
import com.zmn.oms.model.entity.master.OrderMaster;
import com.zmn.oms.services.interfaces.master.OrderMasterService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author sunlife
 * @date:2021/7/23 5:45 下午
 * description:
 */
@Slf4j
@Service(loadbalance = "roundrobin", timeout = 5000, retries = 2, version = DubboConsts.INTERFACE_VERSION, validation = "false")
public class EngineerShareListRemoteServiceImpl implements EngineerShareListRemoteService {
    @Autowired
    OrderMasterService orderMasterService;

    @Override
    public ResponseDTO<List<EngineerShareDRO>> listEngineerShareListByOrderIdAndWorkId(Long orderId, Long workId) {
        try {
            List<OrderMaster> orderMasters = orderMasterService.listMasterByWorkId(orderId, workId);

            List<EngineerShareDRO> engineerList = orderMasters.stream().map(orderMaster -> {
                EngineerShareDRO engineerInfo = new EngineerShareDRO();
                engineerInfo.setEngineerId(orderMaster.getMasterId());
                engineerInfo.setEngineerName(orderMaster.getMasterName());
                engineerInfo.setShareRate(orderMaster.getShareRateBigDecimal());
                return engineerInfo;
            }).collect(Collectors.toList());

            return ResponseDTO.success(engineerList);

        } catch (Exception e) {
            log.info(String.format("[%s]获取师傅分润信息错误:%s", orderId, e.getMessage()), e);
            return ResponseDTO.fail(e.getMessage());
        }
    }
}
